package com.haust.leyou.config;

import com.haust.leyou.utils.RsaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.PublicKey;

/**
 * jwt相关属性 配置类
 */
@Component
@ConfigurationProperties(prefix = "leyou.jwt")
@PropertySource(value = {"classpath:/application.properties"})
public class JwtProperties {

    /**
     * 密钥
     */
//    private String secret;

    /**
     * 公钥路径
     */
    private String pubKeyPath;

    /**
     * 私钥
     */
//    private String priKeyPath;

    /**
     * token过期时间
     */
//    private int expire;

    /**
     * cookie 名称
     */
    private String cookieName;

    /**
     * 公钥
     */
    private PublicKey publicKey;

    /**
     * 私钥
     */
//    private PrivateKey privateKey;

    private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);

    /**
     * @PostContruct：在构造方法执行之后执行该方法
     */
    @PostConstruct
    public void init() {
        try {
//            File pubKey = new File(pubKeyPath);
//            File priKey = new File(priKeyPath);
//            if (!pubKey.exists() || !priKey.exists()) {
//                // 生成公钥和私钥
//                RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
//            }
            // 获取公钥和私钥
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
//            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            logger.error("初始化公钥和私钥失败！", e);
            throw new RuntimeException();
        }
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public void setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
    }

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }
}